<template>
  <view>
    <!-- 搜索区域 -->
    <view class="search-box">
      <my-search @click="gotoSearch" :bgcolor="'pink'" :radius="18"></my-search>
    </view>

    <!-- 轮播图区域 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item,i) in swiperList" :key="i">
        <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>

    <!-- 分类导航区域 -->
    <view class="nav-list">
      <view v-for="(item,i) in navList" :key="i" @click="navClick(item)">
        <image :src="item.image_src" class="nav-img" mode="widthFix"></image>
      </view>
    </view>

    <!-- 楼层区域 -->
    <view class="floor-list">
      <view v-for="(item,i) in floorList" :key="i" class="floor-item">
        <!-- 楼层标题 -->
        <image :src="item.floor_title.image_src" class="floor-img"></image>
        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <!-- 左侧图片 -->
          <navigator class="left-img-box" :url="item.product_list[0].url">
            <image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width+'rpx'}"
              mode="widthFix"></image>
          </navigator>
          <!-- 右侧图片 -->
          <view class="right-img-box">
            <navigator v-for="(item2,i2) in item.product_list" :key="i2" v-if="i2!==0" class="right-img-item"
              :url="item2.url">
              <image :src="item2.image_src" :style="{width:item2.image_width+'rpx'}" mode="widthFix"></image>
            </navigator>
          </view>
        </view>
      </view>
    </view>
  </view>

</template>

<script>
 // 导入自己封装的 mixin 模块
 import badgeMix from '@/mixins/tabbar-badge.js'
 export default {
    // 将 badgeMix 混入到当前的页面中进行使用(挂载)
   mixins:[badgeMix],
    data() {
      return {
        // 存放轮播图的数据
        swiperList: [],
        //存放分类导航的数据
        navList: [],
        //存放楼层数据
        floorList: []
      };
    },
    // 在小程序页面刚加载的时候，调用获取轮播图数据的方法
    onLoad() {
      this.getSwiper(),
        this.getnavList(),
        this.getfloorList()
    },
    methods: {
      //  获取轮播图数据的方法
      async getSwiper() {
        const {
          data: res
        } = await uni.$http.get("https://api-ugo-web.itheima.net/api/public/v1/home/swiperdata")
        //console.log(res.message)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.swiperList = res.message
      },
      //获取分类导航栏
      async getnavList() {
        const {
          data: res
        } = await uni.$http.get("https://api-ugo-web.itheima.net/api/public/v1/home/catitems")
        // console.log(res.message)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.navList = res.message
      },
      //分类被点击处理函数
      navClick(res) {
        if (res.name === '分类') {
          uni.switchTab({
            url: "/pages/cate/cate"
          })
        }
      },
      //定义获取楼层列表数据方法
      async getfloorList() {
        const {
          data: res
        } = await uni.$http.get("https://api-ugo-web.itheima.net/api/public/v1/home/floordata")
        console.log(res.message)
        if (res.meta.status !== 200) return uni.$showMsg()
        res.message.forEach(floor => {
          floor.product_list.forEach(pro => {
            pro.url = '/subpkg/goods_list/goods_list?' + pro.navigator_url.split("?")[1]
          })
        })
        this.floorList = res.message
      },
      gotoSearch() {
        uni.navigateTo({
          url: '/subpkg/serach/serach'
        })
      }
    }

  }
</script>
<style lang="scss">
  //搜索
  .search-box {
    position: sticky;
    top: 0; //固定页面最顶部
    z-index: 599; //显示优先级 防止搜索款下来被轮播图覆盖
  }

  //轮播图
  swiper {
    height: 250rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  // 分类导航
  .nav-list {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    margin: 10rpx 0rpx;

    .nav-img {
      width: 128rpx;
      height: 140rpx;
    }
  }

  //楼层区域
  .floor-img {
    width: 100%;
    height: 60rpx;
  }

  .floor-img-box {
    display: flex;
  }

  .left-img-box {
    padding-left: 15rpx;
  }

  .right-img-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
  }
</style>
